package ch.trainingtest.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import ch.trainingtest.dao.TaskDao;
import ch.trainingtest.dao.TaskHistoryDao;
import ch.trainingtest.dao.UserDao;
import ch.trainingtest.dto.TaskDto;
import ch.trainingtest.dto.UserDto;
import ch.trainingtest.entity.Task;
import ch.trainingtest.entity.TaskHistory;
import ch.trainingtest.entity.User;

@Stateless
public class TaskManagementBean {

	@EJB 
	private TaskDao taskDao;
	
	@EJB
	private UserDao userDao;
	
	@EJB
	private TaskHistoryDao taskHistoryDao;
	
	public List<TaskDto> getAllTasks() {		
		List<Task> allTasks = taskDao.getAllTasks();
		if(allTasks!=null && allTasks.size()>0) {
			List<TaskDto> result = new ArrayList<TaskDto>();
			for(Task task : allTasks) {
				TaskDto taskDto = new TaskDto();
				taskDto.setId(task.getId());
				taskDto.setTitle(task.getTitle());
				taskDto.setDescription(task.getDescription());
				taskDto.setStatus(task.getStatus());
				taskDto.setCreatedBy(getUserDto(task.getCreatedBy()));
				if(task.getAssigneeID()!=0) {
					User assignee = userDao.findUserByID(task.getAssigneeID());
					if(assignee!=null) {
						taskDto.setAssignee(getUserDto(assignee));
					}
					
				}
				result.add(taskDto);
			}
			return result;
		} 
		return null;
	}
	
	public boolean createTask(TaskDto taskDto) {
		Task task = new Task();
		task.setTitle(taskDto.getTitle());
		task.setDescription(taskDto.getDescription());
		task.setStatus(taskDto.getStatus());
		if(taskDto.getCreatedBy()!=null) {
			Integer userID = taskDto.getCreatedBy().getId();
			User createdUser = userDao.findUserByID(userID);
			task.setCreatedBy(createdUser);
			createdUser.getTasks().add(task);
			taskDao.persist(task);
		}
		return true;
		
	}
	
	public boolean createTask(Integer userId, String title, String des, Integer status) {
		Task task = new Task();
		task.setTitle(title);
		task.setDescription(des);
		task.setStatus(status);
		User createdUser = userDao.findUserByID(userId);
		task.setCreatedBy(createdUser);
		createdUser.getTasks().add(task);
		taskDao.persist(task);
		return true;
		
	}
	
	public boolean updateTask(TaskDto taskDto, UserDto modifiedUser) {
		Task task = taskDao.find(taskDto.getId());
		String oldTitle = task.getTitle();
		String oldDes = task.getDescription();
		int oldAssigneeID = task.getAssigneeID();
		
		if(task!=null) {
			task.setTitle(taskDto.getTitle());
			task.setDescription(taskDto.getDescription());
			task.setStatus(taskDto.getStatus());
			if(taskDto.getAssignee()!=null) {
				task.setAssigneeID(taskDto.getAssignee().getId());
			}
			taskDao.merge(task);
			
			//title
			
			if(!taskDto.getTitle().equals(oldTitle)) {
				//add to history
				TaskHistory taskHistory = new TaskHistory();
				taskHistory.setAction("Change title");
				taskHistory.setCreatedTime(new Date());
				taskHistory.setFrom(oldTitle);
				taskHistory.setTo(taskDto.getTitle());
				taskHistory.setModifiedBy(modifiedUser.getUsername());
				taskHistory.setTask(task);
				taskHistoryDao.addHistory(taskHistory);
			}
			
			//des
			
			if(!taskDto.getDescription().equals(oldDes)) {
				//add to history for des
				TaskHistory taskHistory = new TaskHistory();
				taskHistory.setAction("Change description");
				taskHistory.setCreatedTime(new Date());
				taskHistory.setFrom(oldDes);
				taskHistory.setTo(taskDto.getDescription());
				taskHistory.setModifiedBy(modifiedUser.getUsername());
				taskHistory.setTask(task);
				taskHistoryDao.addHistory(taskHistory);
			}
			//assignee
			
			if(taskDto.getAssignee().getId() != oldAssigneeID) {
				//add history
				TaskHistory taskHistory = new TaskHistory();
				taskHistory.setAction("Change assignee");
				taskHistory.setCreatedTime(new Date());
				User oldAssignee = userDao.findUserByID(task.getAssigneeID());
				taskHistory.setFrom(oldAssignee.getUsername());
				taskHistory.setTo(taskDto.getAssignee().getUsername());
				taskHistory.setModifiedBy(modifiedUser.getUsername());
				taskHistory.setTask(task);
				taskHistoryDao.addHistory(taskHistory);
			}
			return true;
			
			
		}
		return false;
	}
	
	private UserDto getUserDto(User user) {
		if(user!=null) {
			UserDto userDto = new UserDto();
			userDto.setId(user.getId());
			userDto.setUsername(user.getUsername());
//			userDto.setPassword(user.getPassword());
			return userDto;
		}
		return null;
	}
}
